/**
 * 
 */
package com.mycompany.client.pages;

import java.util.Date;
import java.util.Map;

import com.mycompany.client.CoralPage;
import com.mycompany.client.dataset.CodeTable;
import com.mycompany.client.widgets.CoralCodeItem;
import com.mycompany.client.widgets.CoralDateItem;
import com.mycompany.client.widgets.CoralHeaderItem;
import com.mycompany.client.widgets.CoralPageTitle;
import com.mycompany.client.widgets.CoralSelectItem;
import com.mycompany.client.widgets.CoralTextItem;
import com.mycompany.client.widgets.form.Coral2ColumnsForm;
import com.mycompany.client.widgets.form.CoralCenterButtonForm;
import com.mycompany.client.widgets.layout.CoralBasicVLayout;
import com.mycompany.shared.PageConstants;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.FormItem;

/**
 * Solve the problem.
 * @author coral.ma
 * 
 */
public class AddSalesActivityPage extends CoralPage {
	
//	private String NAME = "Add Sales Activity";
//	private String DESCRIPTION = "Add Sales Activity";
	
	//should be in POJO.
	private String[] fieldList = new String[]{"activityCategory","agentCode","quotationNo","status",
			"customerCode","customerName","plannedDate","plannedTime","actualDate","actualTime"};
	private Coral2ColumnsForm form = new Coral2ColumnsForm(i18n.basicInfo());
	private CoralDateItem plannedDateItem = new CoralDateItem("plannedDate","Planned Date");
	private CoralDateItem actualDateItem = new CoralDateItem("actualDate","Actual Date");
	
	public static CoralPage create() {
		AddSalesActivityPage activityPage = new AddSalesActivityPage();
		return activityPage;
	}
	
	public Canvas getViewPage() {
		CoralBasicVLayout layout = new CoralBasicVLayout(PageConstants.AddSalesActivityPage);
		CoralPageTitle title = new CoralPageTitle(i18n.addSalesActivityTitle(), "Help testing!");
		layout.addMember(title);
	
		form.setFields(getFormItem());
		setValues(new String[0]);

		layout.addMember(form);
		
		CoralCenterButtonForm buttonForm = new CoralCenterButtonForm();
		if(isWindow) {
			buttonForm.addButtons(windowButtons);
		} else {
			Button button = new Button("Confirm");
			button.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					SC.say("Form.values are : " + form.getValues());
				}
			});
			Button returnBtn = new Button("Return");
			buttonForm.addButtons(button,returnBtn);
		}
		layout.addMember(buttonForm);
		return layout;
	}
	
	public FormItem[] getFormItem() {
		CoralHeaderItem header = new CoralHeaderItem(i18n.basicInfo());
		CoralSelectItem activityCategoryItem = new CoralSelectItem("activityCategory", i18n.activityCategory());
		activityCategoryItem.setValueMap(CodeTable.activityCategory);
		CoralTextItem agentCodeItem = new CoralTextItem("agentCode",i18n.agentCode());
		CoralTextItem quotationNoItem = new CoralTextItem("quotationNo",i18n.quotationNo());
		CoralSelectItem statusItem = new CoralSelectItem("status",i18n.status(),CodeTable.activityStatus);
		CoralCodeItem customerCodeItem = new CoralCodeItem("customerCode",i18n.customerCode());

		CoralTextItem customerNameItem = new CoralTextItem("customerName","CustomerName");
		
//		CoralDateItem plannedDateItem = new CoralDateItem("plannedDate","Planned Date");
//		plannedDateItem.setValue(new Date());
//		CoralTimeItem plannedTimeItem = new CoralTimeItem("plannedTime","Planned Time");
//		actualDateItem = new CoralDateItem("actualDate","Actual Date");
//		actualDateItem.setValue(new Date());
		return new FormItem[]{header, activityCategoryItem, agentCodeItem, quotationNoItem , statusItem, customerCodeItem, customerNameItem, plannedDateItem,  actualDateItem};
	}
	
	public void setValues(String[] values) {
		for(int i=0; i < values.length; i++) {
			form.setValue(fieldList[i], values[i]);
		}
	}
	
	public void setDate(Date startDate, Date endDate) {
		plannedDateItem.setValue(startDate);
		actualDateItem.setValue(endDate);
	}
	
	public Map getValues() {
		return form.getValues();
	}

	public String getPageDescription() {
		return "Add Sales Activity";
	}

	public String getPageName() {
		return "Add Sales Activity";
	}
	
	public String getIcon() {
		return null;
	}
}
